<html>
<head><meta charset="utf-8"><title>UB catched by Miri - no item to reborrow for Unique · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html">UB catched by Miri - no item to reborrow for Unique</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="165336124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165336124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lovecs0079 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165336124">(May 10 2019 at 13:31)</a>:</h4>
<p>Hi, I'm writing a solution for <a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/" target="_blank" title="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">LeetCode Problem 19 - Remove Nth Node From End of List</a>. I tried to use unsafe to implement a O(1) space-consuming O(n) time-consuming solution, however miri throws out an error and I found that this error means there is an illegal alias of a <code>Box</code>. How could I fix the <code>mut_safe_wrapper</code> function to pass miri's test? Thanks!<br>
The code is here: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0948f675999232949f3b0c37ecf4fde2" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0948f675999232949f3b0c37ecf4fde2">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0948f675999232949f3b0c37ecf4fde2</a></p>



<a name="165341625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165341625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165341625">(May 10 2019 at 14:42)</a>:</h4>
<p>Related: <a href="https://stackoverflow.com/q/53242052/155423" target="_blank" title="https://stackoverflow.com/q/53242052/155423">How to remove the Nth node from the end of a linked list?</a></p>



<a name="165343932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165343932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lovecs0079 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165343932">(May 10 2019 at 15:09)</a>:</h4>
<blockquote>
<p>Related: <a href="https://stackoverflow.com/q/53242052/155423" target="_blank" title="https://stackoverflow.com/q/53242052/155423">How to remove the Nth node from the end of a linked list?</a></p>
</blockquote>
<p>Thanks for the link, but I really want to write a fast-slow-link solution with an unsafe to safe wrapper.<br>
BTW, my solution can be compiled and runs fine, however Miri indicates that there is an UB. I want to eliminate that.</p>



<a name="165346190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165346190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165346190">(May 10 2019 at 15:36)</a>:</h4>
<blockquote>
<p>runs fine [...] there is an UB</p>
</blockquote>
<p>You can't have both ;-)</p>



<a name="165346407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165346407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lovecs0079 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165346407">(May 10 2019 at 15:39)</a>:</h4>
<p>Miri said the UB comes from an alias of <code>Box</code>, I double checked my code dropping all const references before <code>transmute</code> to prevent aliases…</p>



<a name="165353421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165353421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165353421">(May 10 2019 at 17:11)</a>:</h4>
<p>the UB seems to be inside an implicit drop</p>



<a name="165353653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165353653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165353653">(May 10 2019 at 17:14)</a>:</h4>
<p>you are casting a shared ref to a mutable ref? I am wondering why this doesnt explode earlier^^</p>



<a name="165354053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165354053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165354053">(May 10 2019 at 17:20)</a>:</h4>
<p>FWIW, <code>drop(prev)</code> cannot possible help, it can only harm. Dropping a mutable ref is basically a NOP, but also counts as a "use" of the ref and hence checks that the ref is still valid. Removing that doesn't help here though.</p>



<a name="165354132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165354132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165354132">(May 10 2019 at 17:20)</a>:</h4>
<p>I am not sure what concretely is the cause here, but transmuting a shared ref to a mutable is a no-go</p>



<a name="165354140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165354140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165354140">(May 10 2019 at 17:20)</a>:</h4>
<p>you cannot have mutable access to a shared ref</p>



<a name="165355779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165355779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165355779">(May 10 2019 at 17:42)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> while I was writing a solution to this, I wanted to convert <code>Option&lt;Box&lt;T&gt;&gt;</code> into a <code>*mut T</code> — is there a straight-forward way of doing that?</p>



<a name="165356360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165356360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165356360">(May 10 2019 at 17:50)</a>:</h4>
<p>hm. don't know. I'd start with <code>.map(Box::into_raw_non_null)</code></p>



<a name="165356381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165356381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165356381">(May 10 2019 at 17:51)</a>:</h4>
<p>there might be things between <code>Option&lt;NonNull&lt;T&gt;&gt;</code> and raw ptrs</p>



<a name="165370643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165370643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165370643">(May 10 2019 at 21:01)</a>:</h4>
<p><code>into_raw...</code> would take ownership, which I wouldn't want. I think that <code>transmute</code> may actually be the shortest point from A to B, which is rarely a good sign ;-)</p>



<a name="165381121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165381121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165381121">(May 10 2019 at 23:55)</a>:</h4>
<p>Once you have a raw pointer, you can copy it and reconstruct the box back.</p>



<a name="165382105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165382105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165382105">(May 11 2019 at 00:13)</a>:</h4>
<p>true, but if I'm starting with e.g. <code>&amp;self</code> i'd run into a problem. I could use <code>as_ref</code> / <code>as_mut</code> of course</p>



<a name="165383215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165383215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lovecs0079 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165383215">(May 11 2019 at 00:41)</a>:</h4>
<blockquote>
<p>you cannot have mutable access to a shared ref</p>
</blockquote>
<p>Thank you Ralf, just known <code>drop(prev)</code> is not helpful</p>



<a name="165526513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/UB%20catched%20by%20Miri%20-%20no%20item%20to%20reborrow%20for%20Unique/near/165526513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/UB.20catched.20by.20Miri.20-.20no.20item.20to.20reborrow.20for.20Unique.html#165526513">(May 13 2019 at 13:11)</a>:</h4>
<p>Ah, I think I finally understood why casting the shared ref to a mutable didn't explode earlier here... you are casting through a raw pointer, and this happens inside a <code>Box</code>. When you use the raw ptr to write to that <code>Box</code>, Miri thinks that this must be the raw ptr originally allocated by <code>exchange_malloc</code> (in <code>Box::new</code>) -- that is the only raw ptr in existence that you are allowed to use for writing. This of course invalidates all pointers created later, including the <code>Box</code> pointer that is later being used for dropping.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>